﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
using System.Xml.Serialization;

namespace Barbar.MapReport.PolygonReducer.Configuration {
  public sealed class PolygonReducerConfig : IConfigurationSectionHandler {
    private static object SyncRoot = new object();
    private static PolygonReducerConfig PolygonReducerConfigInstance;
    private QuerySelect m_QuerySelect = new QuerySelect();
    private string m_DeleteReducedPointsQuery;
    private string m_InsertReducedPointQuery;

    public QuerySelect QuerySelect {
      get { return m_QuerySelect; }
      set { m_QuerySelect = value; }
    }

    public string DeleteReducedPointsQuery {
      get { return m_DeleteReducedPointsQuery; }
      set { m_DeleteReducedPointsQuery = value; }
    }

    public string InsertReducedPointQuery {
      get { return m_InsertReducedPointQuery; }
      set { m_InsertReducedPointQuery = value; }
    }

    public static PolygonReducerConfig Instance {
      get {
        lock (SyncRoot) {
          if (PolygonReducerConfigInstance == null)
            PolygonReducerConfigInstance = (PolygonReducerConfig)ConfigurationManager.GetSection("PolygonReducerConfig");
          return PolygonReducerConfigInstance;
        }
      }
    }

    #region IConfigurationSectionHandler Members

    object IConfigurationSectionHandler.Create(object parent, object configContext, XmlNode section) {
      XmlSerializer serializer = new XmlSerializer(typeof(PolygonReducerConfig));
      PolygonReducerConfig config = null;
      using (XmlNodeReader reader = new XmlNodeReader(section)) {
        config = (PolygonReducerConfig)serializer.Deserialize(reader);
        reader.Close();
      }
      return config;
    }

    #endregion
  }
}
